<div class="chat-container">
    <div class="chat-workflow">
        <p>&gt;&gt; 12:12 message</p>
    </div>
    <div class="chat-control">
        <input type="text" id="message">
        <button id="send-message">Submit</button>
    </div>
</div>

<?php
// Yii::app()->clientScript->registerScriptFile($this->assetsUrl . '/swfobject.js',  CClientScript::POS_END);
// Yii::app()->clientScript->registerScriptFile($this->assetsUrl . '/web_socket.js', CClientScript::POS_END);
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerCssFile('/css/style.css');

/*Yii::app()->clientScript->registerScript('websocket-test', '
    // Let the library know where WebSocketMain.swf is:
    WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";

    // Write your code in the same way as for native WebSocket:
    var ws = new WebSocket("ws://websockets-test.local:8000/demo");
        ws.onopen = function() {
        ws.send("Hello");  // Sends a message.
    };
    ws.onmessage = function(e) {
    // Receives a message.
        alert(e.data);
    };
    ws.onopen = function() {
        alert("opened");
    }
    ws.onclose = function() {
        alert("closed");
    };

    var channelId = 12345;
    function check_messages() {
        $.get("/listen?cid=" + channelId, {}, function(r) {
            // Считаем, что у нас есть div c id=messages,
            // куда мы дописываем сообщения
            $("#messages").append(r);
            setTimeout(check_messages, 500);
        }, "json");
    }
', CClientScript::POS_END);*/
Yii::app()->clientScript->registerScript('send-message', '
    var channelId = 12345;

    function check_messages() {
        $.get("/listen?cid=" + channelId + "&" + Date.now(), {}, function(r) {
            $(".chat-workflow p:first").before(r);
            setTimeout(check_messages, 500);
        }, "json");
    }

    check_messages();

    $("#send-message").on("click", function() {
        var text = $("#message").val();

        if (text.length > 0) {
            $.ajax({
                url: "/processMessage",
                type: "POST",
                data: {message: text},
                success: function(result){
                    if (result.status == "ok") {
                        $("#message").val("");
                    }
                },
                error: function(error) {
                }
            });
        }
    });
', CClientScript::POS_END);
